#include "../stdafx.h"
#include "MobileTile.h"
#include<exception>


namespace Model{
	MobileTile::MobileTile(Treasure* tr,bool exit1,bool exit2,bool exit3,bool exit4,Orientation ori):Tile(tr,exit1,exit2,exit3,exit4,ori)
	{
	}
	MobileTile::MobileTile(bool exit1,bool exit2,bool exit3,bool exit4,Orientation ori):Tile(exit1,exit2,exit3,exit4,ori)
	{
	}

	MobileTile* MobileTile::goRight()
	{
		if(this->rightNeighbor()==NULL){
			return this;
		}
		else{
			MobileTile* rightNeighbor=dynamic_cast<MobileTile*>(this->rightNeighbor()); //if this->neighborRight() is a MobileTile* then
			//rightNeighbor=this->neighborRight()
			//else rightNeighbor=0
			if(rightNeighbor){
				this->setUpNeighbor(rightNeighbor->upNeighbor());
				this->setDownNeighbor(rightNeighbor->downNeighbor());
				return rightNeighbor->goRight();
			}
			//TODO faire une exception mieux
			else throw "Trying to push a Fix Tile";
		}
	}

	MobileTile* MobileTile::goLeft()
	{
		if(this->leftNeighbor()==NULL){
			return this;
		}
		else{
			MobileTile* leftNeighbor=dynamic_cast<MobileTile*>(this->leftNeighbor()); //if this->neighborLeft() is a MobileTile* then
			//leftNeighbor=this->neighborLeft()
			//else leftNeighbor=0
			if(leftNeighbor){
				this->setUpNeighbor(leftNeighbor->upNeighbor());
				this->setDownNeighbor(leftNeighbor->downNeighbor());
				return leftNeighbor->goLeft();
			}
			//TODO faire une exception mieux
			else throw "Trying to push a Fix Tile";
		}
	}

	MobileTile* MobileTile::goUp()
	{
		if(this->upNeighbor()==NULL){
			return this;
		}
		else{
			MobileTile* upNeighbor=dynamic_cast<MobileTile*>(this->upNeighbor()); //if this->neighborUp() is a MobileTile* then
			//upNeighbor=this->neighborUp()
			//else upNeighbor=0
			if(upNeighbor){
				this->setRightNeighbor(upNeighbor->rightNeighbor());
				this->setLeftNeighbor(upNeighbor->leftNeighbor());
				return upNeighbor->goUp();
			}
			//TODO faire une exception mieux
			else throw "Trying to push a Fix Tile";
		}
	}

	MobileTile* MobileTile::goDown()
	{
		if(this->downNeighbor()==NULL){
			return this;
		}
		else{
			MobileTile* downNeighbor=dynamic_cast<MobileTile*>(this->downNeighbor()); //if this->neighborDown() is a MobileTile* then
			//downNeighbor=this->neighborDown()
			//else downNeighbor=0
			if(downNeighbor){
				this->setRightNeighbor(downNeighbor->rightNeighbor());
				this->setLeftNeighbor(downNeighbor->leftNeighbor());
				return downNeighbor->goDown();
			}
			//TODO faire une exception mieux
			else throw "Trying to push a Fix Tile";
		}
	}
	void MobileTile::setOrientation(Orientation ori)
	{
		this->ori=ori;
	}

	Orientation MobileTile::getOrientation()
	{
		return this->ori;
	}
	MobileTile::~MobileTile(void)
	{
	}
}
